package com.gzc.just.play.last.war.robotclient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 机器人客户端应用程序
 * 基于C++ RobotConsole应用实现
 */
@SpringBootApplication(exclude = {
    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
    org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
})
@EnableDiscoveryClient
@EnableConfigurationProperties
@EnableScheduling
public class RobotClientApplication implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(RobotClientApplication.class);
    
    @Autowired
    private RobotClientManager robotManager;
    
    @Autowired
    private RobotClientProperties properties;
    
    public static void main(String[] args) {
        try {
            logger.info("Starting Robot Client Application...");
            SpringApplication.run(RobotClientApplication.class, args);
        } catch (Exception e) {
            logger.error("Error running Robot Client Application", e);
        }
    }
    
    @Override
    public void run(String... args) throws Exception {
        if (!properties.isEnabled()) {
            logger.info("Robot client is disabled, exiting");
            return;
        }
        
        logger.info("Robot client application started with properties: {}", properties);
        
        // 初始化机器人管理器
        robotManager.initialize();
        
        // 创建初始机器人
        if (properties.getInitialCount() > 0) {
            RobotClientConfig templateConfig = new RobotClientConfig();
            templateConfig.setRobotId(properties.getInitialRobotId());
            templateConfig.setUsername(properties.getRobotDefault().getUsernamePrefix());
            templateConfig.setPassword(properties.getRobotDefault().getPassword());
            templateConfig.setNickname(properties.getRobotDefault().getNicknamePrefix());
            templateConfig.setSex(properties.getRobotDefault().getSex());
            templateConfig.setHeaderId(properties.getRobotDefault().getHeaderId());
            templateConfig.setPlatformId(properties.getRobotDefault().getPlatformId());
            templateConfig.setLoginServerHost(properties.getLoginServer().getHost());
            templateConfig.setLoginServerPort(properties.getLoginServer().getPort());
            templateConfig.setGateServerHost(properties.getGateServer().getHost());
            templateConfig.setGateServerPort(properties.getGateServer().getPort());
            templateConfig.setEnableAI(true);
            templateConfig.setAiType(1);
            
            robotManager.createRobots(properties.getInitialCount(), templateConfig);
            
            logger.info("{} initial robots created successfully", properties.getInitialCount());
        }
        
        // 添加关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            logger.info("Shutting down robot client application...");
            robotManager.cleanup();
            logger.info("Robot client application shutdown completed");
        }));
        
        logger.info("Robot client application initialization completed");
    }
}